package Service;

import Dao.JournalismMapper;
import Dao.UserMapper;
import Mybatis_until.MybaitsUntil;
import Pojo.Journalism;
import Pojo.User;
import Pojo.NewInfo;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class ViceCheck {

    static Logger logger = Logger.getLogger(UserMapper.class);

    //    检查用户名密码的正确性
    public boolean check(String username, String password) {
        SqlSession sqlSession = MybaitsUntil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.check(username, password);
        sqlSession.close();
//        logger.info(user);
        if (user != null) {
            return true;
        } else {
            return false;
        }

    }

    //    注册账户
    public void regist(String username, String password, String phone) {
        SqlSession sqlSession = MybaitsUntil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = new User(username, password, phone,"");
        int x = userMapper.adduser(user);
        if (x > 0) {
            return;
        }
        sqlSession.close();
    }

    //    统计新闻信息
    public Journalism Journalisminfo() {
        SqlSession sqlSession = MybaitsUntil.getSqlSession();
        JournalismMapper j = sqlSession.getMapper(JournalismMapper.class);
        Journalism journalism = new Journalism(j.usernum(), j.newnum(), j.tagnum());
        sqlSession.close();
        return journalism;


    }

    //    查找新闻内容
    public List<NewInfo> findinfo(int index) {
        SqlSession sqlSession = MybaitsUntil.getSqlSession();
        JournalismMapper j = sqlSession.getMapper(JournalismMapper.class);
        List<NewInfo> list = new ArrayList<>();
        list=j.info(index,10);
        sqlSession.close();
        return list;
    }
}
